<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { loginApi, codeBtnApi } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRouter, useRoute } from 'vue-router'
import { useSendMobileCode } from '@/composables'

const store = useUserStore()
const router = useRouter()
const route = useRoute()

const mobile = ref('13230000079')
const password = ref('abc12345')
const agree = ref(false)
const pedIcon = ref(false)

const onSubmit = async () => {
  if (!agree.value) return showToast('请勾选协议')
  const res = isPass.value
    ? await loginApi(mobile.value, password.value)
    : await codeBtnApi(mobile.value, code.value)

  store.setUser(res.data)
  showSuccessToast('登录成功')

  // router.replace替换当前路由 不增加历史路由
  // route.query.returnUrl带查询参数的路由 回退之前页面
  router.replace((route.query.returnUrl as string) || '/user')
}

// 短信登录
const isPass = ref(true) //TRUE 密码登录 FALSE 短信登录
const code = ref('')
const { form, time, codeBtn } = useSendMobileCode(mobile, 'login')

onMounted(() => {})

// http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback
// https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=
const url =
  'https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=' +
  encodeURIComponent(import.meta.env.VITE_APP_CALLBACK + '/login/callback')
</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册" @click-right="$router.push('/register')"></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信登录' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit" ref="form">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="mobileRules"
        v-model="mobile"
      ></van-field>
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        :type="pedIcon ? 'text' : 'password'"
        :rules="passwordRules"
        v-model="password"
      >
        <template #button>
          <cp-icon
            :name="pedIcon ? 'login-eye-on' : 'login-eye-off'"
            @click="pedIcon = !pedIcon"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        v-if="!isPass"
        placeholder="请输入短信验证码"
        type="text"
        :rules="codeRules"
        v-model="code"
      >
        <template #button>
          <span class="btn-send" @click="codeBtn" :class="{ active: time > 0 }">{{
            time > 0 ? `${time}s后在发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>

      <div class="icon">
        <a @click="store.setReturnUrl($route.query.returnUrl as string)" :href="url"
          ><img src="@/assets/qq.svg" alt=""
        /></a>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
.van-form {
  padding: 0 14px;

  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;

    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }

  .btn-send {
    color: var(--cp-primary);

    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
